package com.ysx.oa.delay.controller;


import com.ysx.oa.delay.bean.DelayBean;
import com.ysx.oa.delay.service.DelayService;
import com.ysx.oa.user.bean.UserBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/delay")
public class DelayController {


    @Autowired
    private DelayService service;




    /*
     * 方法描述:批量添加学生迟到记录
     * 测试网页是src/main/kaoqin/fyh-kaoqin-web/addDelay.html 本地访问
     * 注意可能有跨域问题 可以在com.ysx.oa.student.controller.StudentController类之中的findStudentList方法添加注解@CrossOrigin
     * @author: 方雅浩
     * @date: 2021/11/23 15:56
     * @params:
     * @param beanList:
     * @returns:java.util.Map<java.lang.String,java.lang.Object>
     */
    @CrossOrigin
    @RequestMapping(value = "/add.do" ,method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> add(@RequestBody List<DelayBean> beanList, HttpServletRequest request){
        Map<String,Object> map = new HashMap<>();

        HttpSession session = request.getSession();
        Integer userId = (Integer) session.getAttribute("userId");
        Integer roleId =(Integer) session.getAttribute("roleId");

        if(roleId!=20040){
            map.put("code",405);
            map.put("message","没有权限");
        }else{
            int add = service.add(beanList,userId);
            if(add!=0){
                map.put("code",200);
                map.put("message","success");
            }
        }

        return map;

    }

    @CrossOrigin
    @RequestMapping(value = "/addInit.do" ,method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> addInit( HttpServletRequest request){
        Map<String,Object> map = new HashMap<>();

        HttpSession session = request.getSession();
        Integer userId = (Integer) session.getAttribute("userId");
        Integer roleId =(Integer) session.getAttribute("roleId");



        if(roleId!=20040){
            map.put("code",405);
            map.put("message","没有权限");
        }else{
            List<UserBean> userBeans = service.addInit(userId);
            if(userBeans!=null){
                map.put("code",200);
                map.put("data",userBeans);
                map.put("message","success");
            }
        }

        return map;

    }


    @RequestMapping(value = "/update.do" ,method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> update( @RequestBody Map<String,DelayBean> bean){
        Map<String,Object> map = new HashMap<>();
        String newDelayDate =bean.get("new").getDelayDate();
        int update = service.updateDelayRecord(bean.get("old"), newDelayDate);
        if(update!=0){
            map.put("code",200);
            map.put("message","success");
        }

        return map;

    }

    @RequestMapping(value = "/delete.do" ,method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> delete( @RequestBody DelayBean bean){
        Map<String,Object> map = new HashMap<>();

        int delete = service.deleteDelayRecord(bean);

        if(delete!=0){
            map.put("code",200);
            map.put("message","success");
        }
        return map;

    }

    @RequestMapping(value = "/find.do" ,method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> find( @RequestBody DelayBean bean){
        Map<String,Object> map = new HashMap<>();

        List<DelayBean> delayRecord = service.findDelayRecord(bean);
        map.put("result",delayRecord);
        return map;

    }




}
